#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qstack.h>
#include <qstring.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置界面大小，及标题
    this->setMaximumSize(220,320);
    this->setMinimumSize(220,320);
    this->setWindowTitle("计算器");

    //设置显示字体
    QFont f("微软雅黑",18);
    ui->mainLineEdit->setFont(f);

    //设置删除按键图片
    QIcon con("E:\\Qt\\WorkPace\\calculator\\del.jpg");//创建图片对象
     ui->delButton->setIcon(con);//设置按钮图片

    //设置按钮颜色
     ui->equalButton->setStyleSheet("color: rgb(0, 0, 0); background-color: rgb(72,209,204);");

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_oneButton_clicked()
{
    expression += "1";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_twoButton_clicked()
{
    expression += "2";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_threeButton_clicked()
{
    expression += "3";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_fourButton_clicked()
{
    expression += "4";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_fiveButton_clicked()
{
    expression += "5";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_sixButton_clicked()
{
    expression += "6";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_sevenButton_clicked()
{
    expression += "7";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_eightBUtton_clicked()
{
    expression += "8";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_nineButton_clicked()
{
    expression += "9";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_zeroButton_clicked()
{
    expression += "0";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_leftButton_clicked()
{
    expression += "(";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_ringhtButton_clicked()
{
    expression += ")";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_addButton_clicked()
{
    expression += "+";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_subButton_clicked()
{
    expression += "-";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_mulButton_clicked()
{
    expression += "*";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_divButton_clicked()
{
    expression += "/";
    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_clearButton_clicked()
{
    // expression = "";//字符串清空
    expression.clear();//字符串清空
    ui->mainLineEdit->clear();
}


void MainWindow::on_delButton_clicked()
{
    //QString 移除最后一个字符3个方法

    expression.chop(1);//直接移除最后一个
    // expression = expression.left(expression.size()-1);//显示左边多少个
    // expression = expression.remove(expression.size()-1,1);//从某位移除多少个

    ui->mainLineEdit->setText(expression);
}


void MainWindow::on_equalButton_clicked()
{
    //计算字符串
    //先计算出后缀表达式
    QString s1=InfixToPostfix(expression);
    //再计算后缀表达式
    double d1=EnfixToPostfix(s1);


    ui->mainLineEdit->setText(QString::number(d1));//计算结果转换成字符串显示
    //清空字符串
    expression.clear();

}

// 运算符优先级
int MainWindow:: precedence(QChar c) {
    if (c == '+' || c == '-') {
        return 1;
    }
    else if (c == '*' || c == '/') {
        return 2;
    }
    else {
        return 0;
    }
}


/*
*函数功能：将中缀表达式转换为后缀表达式
* 参数：中缀表达式(字符串形式)
* 返回值：后缀表达式(字符串形式)
*/
QString MainWindow::InfixToPostfix(QString expression)
{
    QString postfix;// 后缀表达式
    QStack<char>  s1 ;
    for (int i = 0; i < expression.length(); i++)
    {
        QChar c1 = expression[i];
        if (c1>='0'&&c1<='9') {
            // 数字入栈
            postfix += c1;
        }
        else if (c1 == '(') {
            // 左括号入栈
            s1.push(static_cast<char>(c1.unicode()));
        }
        else if (c1 == ')') {
            // 右括号出栈，计算表达式，结果入栈
            while (s1.top()!= '(') {
                postfix += s1.top();
                s1.pop();
            }
            s1.pop();
        }
        else {
            // 运算符出栈，直到遇到比它优先级低的运算符，然后入栈
            while (!s1.empty() && precedence(c1) <= precedence(s1.top())) {
                postfix += s1.top();
                s1.pop();
            }
            s1.push(static_cast<char>(c1.unicode()));
        }
    }
    // 表达式扫描完成，剩余运算符出栈，计算结果，结果入栈
    while (!s1.empty()) {
        postfix += s1.top();
        s1.pop();
    }

    return postfix;
}


/*
* 函数功能：计算后缀表达式
* 参数：后缀表达式(字符串形式)
* 返回值：计算结果(double类型)
*/
double MainWindow :: EnfixToPostfix(QString postfix)
{
    QStack<double> valueStack;
    for (int i = 0; i < postfix.length(); i++) {
        QChar c2 = postfix[i];
        if (c2 >= '0' && c2 <= '9') {
            // 数字入栈
            valueStack.push(static_cast<char>(c2.unicode()) - '0');
        }
        else {
            // 运算符出栈，计算结果，结果入栈
            double b = valueStack.top();
            valueStack.pop();
            double a = valueStack.top();
            valueStack.pop();
            if (c2 == '+') {
                valueStack.push(a + b);
            }
            else if (c2 == '-') {
                valueStack.push(a - b);
            }
            else if (c2 == '*') {
                valueStack.push(a * b);
            }
            else if (c2 == '/') {
                valueStack.push(a / b);
            }
        }
    }


    return valueStack.top();// 访问栈顶元素;// 返回结果
}


